﻿
using IoTClient;
using IoTClient.Clients.PLC;
using IoTClient.Common.Enums;
using IoTClient.Enums;
using Microsoft.Extensions.DependencyInjection;
using Sunny.UI;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apple.SprayingProcessSystem
{
    public static class Globals
    {
        // 全局服务提供者
        public static ServiceProvider ServiceProvider;

        // 初始化ini文件
        public static IniFile IniFile = new IniFile(Application.StartupPath + "config.ini");

        // PLC配置文件路径
        public static string PlcVarConfigPath;

        // 删除日志文件的路径
        public static string DelFilePath;

        // 删除日志文件天数
        public static string SaveDay;

        // 软件版本
        public static string SoftwareVersion;

        // 软件试用时间
        public static string SYTime;

        //IotClient 西门子PLC操作对象
        public static SiemensClient SiemensClient;

        //CPU类型
        public static SiemensVersion CpuType;
        //IP地址
        public static string IpAddress;
        //端口号
        public static int Port;
        //插槽号
        public static byte Slot;
        //机架号
        public static byte Rack;
        //PLC连接超时时间
        public static int ConnectTimeOut;
        //PLC读取间隔时间
        public static int ReadTimeInterval;
        //PLC重连时间间隔
        public static int ReConnectTimeInterval;
        // PLC变量字典
        public static Dictionary<string, DataTypeEnum> ReadDic = new Dictionary<string, DataTypeEnum>();
        // PLC变量值 读取的 字典
        public static Dictionary<string, object> DataDic = new Dictionary<string, object>();
        // PLC变量 写入的 字典
        public static Dictionary<string, string> WriteDic = new Dictionary<string, string>();
        // PLC变量 需要保存的变量集合
        public static List<string> SaveList = new List<string>();

        /// <summary>
        /// PLC写入数据
        /// </summary>
        /// <param name="varName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool PlcWrite(string varName, object value, DataTypeEnum dataType = DataTypeEnum.Bool)
        {
            if (SiemensClient == null || !SiemensClient.Connected)
            {
                return false;
            }

            // 根据名称拿到对应的PLC地址
            var address = WriteDic[varName];
            Result res = SiemensClient.Write(address, value, dataType);

            return res.IsSucceed;
        }

        #region 获取所有控件
        // 获取指定页面的所有控件
        public static List<Control> GetAllControls(UIPage page)
        {
            List<Control> allControls = new List<Control>();
            CollectControls(page.Controls, allControls);
            return allControls;
        }

        // 递归方法，用于收集控件
        private static void CollectControls(Control.ControlCollection controls, List<Control> allControls)
        {
            foreach (Control control in controls)
            {
                allControls.Add(control); // 将当前控件添加到列表中
                // 如果当前控件有子控件，则递归调用此方法
                if (control.HasChildren)
                {
                    CollectControls(control.Controls, allControls);
                }
            }
        }
        #endregion

    }
}
